package com.tedu.springmvc2.controller;

import com.tedu.springmvc2.mapper.UserMapper;
import com.tedu.springmvc2.pojo.dto.UserDTO;
import com.tedu.springmvc2.pojo.dto.UserUpdateDTO;
import com.tedu.springmvc2.pojo.entity.User;
import com.tedu.springmvc2.pojo.vo.UserVO;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

//@Controller
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    /*@RequestMapping("/users/reg")
    @ResponseBody*/
    @RequestMapping("reg")
    public String reg(UserDTO userDTO) {
        /*System.out.println("开始处理注册");*/
        User user = new User();
        /*user.setUsername(userDTO.getUsername());
        user.setPassword(userDTO.getPassword());
        user.setNickname(userDTO.getNickname());*/
        BeanUtils.copyProperties(userDTO, user);//复制属性值
        user.setCreated(new Date());
        userMapper.insertUser(user);
        System.out.println(user);
        return "注册成功";
    }

    /*@RequestMapping("/users/update")
    @ResponseBody*/
    @RequestMapping("update")
    public String update(UserUpdateDTO userUpdateDTO) {
        User user = new User();
        BeanUtils.copyProperties(userUpdateDTO, user);
        System.out.println(user);
        int num = userMapper.updateById(user);
        return num>0 ? "修改成功":"修改失败";
    }
   /* @RequestMapping("users/delete")
    @ResponseBody*/
   @RequestMapping("delete")
    public String delete(Integer id) {
        System.out.println("用户删除操作!id为:"+id);
        int num = userMapper.deleteById(id);
        return num > 0 ? "删除成功" : "删除失败";
    }
    /*@RequestMapping("users/findAll")
    @ResponseBody*/
    @RequestMapping("findAll")
    public List<UserVO> findAll() {
        return userMapper.selectAll();
    }
}
